using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GUI_Webpage_Food : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }

    private void LoadData()
    {
        FoodBll foodBll = new FoodBll();
        try
        {
            SqlDataReader reader = foodBll.ListAllFood();
            grvFood.DataSource = reader;
            grvFood.DataBind();

            //RestaurantBll resBll = new RestaurantBll();
            //resBll.LoadRestaurant(drpRestaurant);
            TypeOfFoodBll typeFoodBll = new TypeOfFoodBll();
            typeFoodBll.LoadTypeOfFood(drpTypeFood);
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        FoodInfo foodInfo = new FoodInfo();
        FoodBll foodBll = new FoodBll();

        foodInfo.FoodID = txtFoodID.Text;
        foodInfo.FoodName = txtFoodName.Text;
        foodInfo.NumberOfPerson = Convert.ToInt32(txtNumberPerson.Text);
        foodInfo.Price = float.Parse(txtPrice.Text);
        foodInfo.FoodPicture = fileFood.FileName;
        foodInfo.FoodDescription = txtDescription.Text;

        if (foodBll.CreateFood(foodInfo) > 0)
        {
            LoadData();
            DeleteBlank();
        }
    }
    protected void grvFood_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvFood.EditIndex = (int)e.NewEditIndex;
        LoadData();
    }
    protected void grvFood_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvFood.EditIndex = -1;
        DeleteBlank();
        LoadData();
    }
    protected void grvFood_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        FoodInfo foodInfo = new FoodInfo();
        FoodBll foodBll = new FoodBll();
        foodInfo.FoodID = grvFood.DataKeys[e.RowIndex].Value.ToString();
        foodBll.DeleteFood(foodInfo);
        DeleteBlank();
        LoadData();
    }
    protected void grvFood_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        FoodInfo foodInfo = new FoodInfo();
        FoodBll foodBll = new FoodBll();
        string typeName = ((TextBox)grvFood.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string numberPerson = ((TextBox)grvFood.Rows[e.RowIndex].Cells[3].Controls[0]).Text;  
        string price = ((TextBox)grvFood.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        TypeOfFoodBll typeOfFoodBll = new TypeOfFoodBll();
        TypeOfFoodInfo typeOfFoodInfo = new TypeOfFoodInfo();
        typeOfFoodInfo.TypeName = typeName;
        
        foodInfo.FoodID = ((TextBox)grvFood.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        foodInfo.FoodName = ((TextBox)grvFood.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        foodInfo.TypeID = Int32.Parse(typeOfFoodBll.ListTypeID(typeOfFoodInfo));
        foodInfo.Price = float.Parse(price);
        foodInfo.FoodDescription = ((TextBox)grvFood.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

        grvFood.EditIndex = -1;

        foodBll.UpdateFood(foodInfo);
        DeleteBlank();
        LoadData();
    }

    #region Clear control text to blank
    private void DeleteBlank()
    {
        txtDescription.Text = "";
        txtFoodID.Text = "";
        txtFoodName.Text = "";
        txtPrice.Text = "";
        //drpTypeFood.Text = "";
        //drpRestaurant.Text = "";
    }
    #endregion
}
